<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-8-3
 * Time: 下午3:57
 */
use \PFT\Db;
use \PFT\Resource;
include_once '/var/www/html/new/d/common/func.inc.php';
include_once '/var/www/html/new/d/module/common/Db.class.php';
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
Db::Conf($dbConf['localhost']);

include '../../class/Api.class.php';
include 'Resource.class.php';
$obj = new Resource(Db::Connect());

Api::SetJsonHeader();
//保存数据：更新和新建
//if ($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST)) {
//    $res = $obj->SaveResource();
//    if ($res===true) Api::Response('ok', Api::okCode);
//    Api::Response('fail', Api::badRequestCode);
//}
$action = $_REQUEST['action'];
switch($action) {
    case 'SAVE_RESOURCE':
        include_once '../../common/func.inc.php';

        if(!$_POST['title'])   Api::Response('请输入资源名称', Api::badRequestCode);
        if($_POST['jtype']=='') Api::Response('请输入资源等级', Api::badRequestCode);//可能是0  不能判断
        if(!$_POST['d_province'] || !$_POST['d_city'])
            Api::Response('请输入省市信息', Api::badRequestCode);
        if(!$_POST['address']) Api::Response('请输入地址信息', Api::badRequestCode);
        $tel_mark = isphone($_POST['tel'])||ismobile($_POST['tel']);

        if(!empty($_POST['tel']) && !$tel_mark)
                                   Api::Response('请输入正确的电话号码或不填', Api::badRequestCode);
        /*
       if(!$_POST['runtime'])     Api::Response('请输入开放时间', Api::badRequestCode);
       if(!$_POST['topic'])   Api::Response('请输入主题', Api::badRequestCode);
       */
        if(!$_POST['lng_lat_pos']) Api::Response('请输入经纬度', Api::badRequestCode);
        if(!$_POST['imgpathGrp'])  Api::Response('请上传资源图片', Api::badRequestCode);
        if(!$_POST['title'])       Api::Response('请输入资源名称', Api::badRequestCode);
        $res = $obj->SaveResource();
        if ($res===true) Api::Response('ok', Api::okCode);
        Api::Response($res['msg'], Api::badRequestCode);
        break;
    /**
     * 获取主题
     */
    case 'TOPIC':
        $topic = file_get_contents('../common/topic.txt');
        $topic_arr = explode("\n", $topic);
        if ($_SERVER['REQUEST_METHOD']=='GET') {
            Api::CollectionResponse($topic_arr);
        }
        elseif ($_SERVER['REQUEST_METHOD']=='POST') {
            $topic = safetxt($_POST['topic']);
            if (!in_array($topic, $topic_arr)) {
                file_put_contents('../common/topic.txt', $topic.PHP_EOL, FILE_APPEND);
            }
            echo '{"code":200}';
        }
        break;
    /**
     * 查询竞争对手
     * action:QueryMember
     * param: 6位数字/11位数字/字符
     */
    case 'QUERY_MEMBER':
        $param = safetxt($_GET['param']);
        if (is_numeric($param) ) {
            $len = strlen($param);
            if ($len==6) $query = "account=?";
            elseif ($len==11) $query = "mobile=?";
        } else {
            $query = "dname LIKE '%?%'";
        }
        $sql = "SELECT id,dname FROM pft_member WHERE {$query}";
        $stmt = Db::Connect()->prepare($sql);
        $stmt->execute(array($param));
        $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if ($data) {
            Api::CollectionResponse($data);
        }
        Api::Response('没有数据', Api::noContentCode);
        break;

    /**
     * 根据地区查询资源
     * action:QUERY_RESOURCE
     * prov:省份ID，
     * city:市ID
     */
    case 'QUERY_RESOURCE':
        $prov = intval($_GET['prov']);
        $city = intval($_GET['city']);
        if(
            $_GET['prov']==1||$_GET['prov']==2||$_GET['prov']==3||
            $_GET['prov']==4 ||$_GET['prov']==32||$_GET['prov']==33 ||$_GET['prov']==34
        ){
            $city = 1;
        }
        $ret  = $obj->GetResourceByArea($prov, $city);
        if ($ret) {
            Api::CollectionResponse($ret);
        }
        Api::Response('没有数据', Api::noContentCode);
        break;
    /**
     * 获取资源详情
     * action:QUERY_RESOURCE_DETAIL
     * id: 资源ID
     */
    case 'QUERY_RESOURCE_DETAIL':
        $resource_id = intval($_GET['id']);
        try {
            $data = $obj->GetResourceInfo($resource_id);
            Api::CollectionResponse($data);
        } catch (ErrorException $e) {
            Api::Response('没有数据', Api::noContentCode);
        }
        break;
    case 'DELETE_RESOURCE':
        $id = intval($_POST['id']);
        $ret = $obj->Rm($id);
        echo $ret===true ? 1 : 0;
        break;
}
